var pageNum=1,pageSize=5,pageTotal=0;
$(function(){
	loadAllRecMsg();
	
	$("#insertBut").click(function(){
		var recCon = $("#myInsertMessage").serialize();
		var rec_title = $("#rec_title").val();//标题
		if(rec_title==""){
			$("#rec_title").css("border","1px solid red");
			$("#alterContent").text("标题不能为空！！！");
			loadAlter();
			return false;
		}
		var rec_duty = $("#rec_duty").val();//招聘岗位
		if(rec_duty==""){
			$("#rec_duty").css("border","1px solid red");
			$("#alterContent").text("招聘岗位不能为空！！！");
			loadAlter();
			return false;
		}
		var rec_welfare = $("#rec_welfare").text();//福利待遇
		if(rec_welfare==""){
			$("#rec_welfare").css("border","1px solid red");
			$("#alterContent").text("福利待遇不能为空！！！");
			loadAlter();
			return false;
		}
		var rec_company_detail = $("#rec_company_detail").text();//岗位要求详细
		if(rec_company_detail==""){
			$("#rec_company_detail").css("border","1px solid red");
			$("#alterContent").text("岗位要求详细不能为空！！！");
			loadAlter();
			return false;
		}
		utilAjax("/recruitment_msg/insertRecMsg",recCon,"post",
				function(data){
			$("#alterContent").text(data.msg);
			if(data.msg=="发布成功"){
				$("#insertRecTable").hide();
				$("#recTable").show();
				loadAllRecMsg();
			}
			loadAlter();
		});
	});
	$("#alterBut").click(function(){
		$("#alterDiv").hide();
	});
});

function loadAllRecMsg(){
	utilAjax("/recruitment_msg/queryAllRecMsgByRecruiter_id","pageNum="+pageNum+"&pageSize="+pageSize,"get",
			function(data){
		pageNum = data.pageNum;
		pageSize = data.pageSize;
		pageTotal = data.pages;
		parseAllRecMsg(data.list);
	});
}
function parseAllRecMsg(lists){
	var zpCon = "";
	if(lists!=""){
		zpCon+="<tr><td colspan='16' align='center' style='border:none;'><h2>所有招聘信息</h2></td></tr>" +
			"<tr class='info text-info'><th>序号</th><th>标题</th><th>联系电话</th><th>薪资范围</th>"+
			"<th>招聘岗位</th><th>招聘者姓名</th><th class='center-block'>工作年限</th>"+
			"<th>学历要求</th><th>福利待遇</th><th>岗位详细</th><th>状态</th>"+
			"<th>审核状态</th><th>公司名</th><th>公司地址</th><th>创建时间</th><th>操作</th>"+
		"</tr>";
		$.each(lists,function(i,r){
			var rec_workYear = "";//设置经验年限
			if(r.rec_workYear==1){rec_workYear = "在校生"}
			if(r.rec_workYear==2){rec_workYear = "应届生"}
			if(r.rec_workYear==3){rec_workYear = "一年以内"}
			if(r.rec_workYear==4){rec_workYear = "1-5年"}
			if(r.rec_workYear==5){rec_workYear = "5-10年"}
			if(r.rec_workYear==6){rec_workYear = "10年以上"}
			var rec_qualification = "";//设置学历要求
			if(r.rec_qualification==1){rec_qualification = "初中及以下"}
			if(r.rec_qualification==2){rec_qualification = "中专/技校"}
			if(r.rec_qualification==3){rec_qualification = "高中"}
			if(r.rec_qualification==4){rec_qualification = "大专"}
			if(r.rec_qualification==5){rec_qualification = "本科"}
			if(r.rec_qualification==6){rec_qualification = "硕士"}
			if(r.rec_qualification==7){rec_qualification = "博士"}
			var rec_msg_check_state = "";
			if(r.rec_msg_check_state==1){rec_msg_check_state = "通过"}
			if(r.rec_msg_check_state==2){rec_msg_check_state = "未通过"}
			if(r.rec_msg_check_state==3){rec_msg_check_state = "审核中"}
			zpCon += "<tr>" +
					"<td>"+r.rec_id+"</td>" +
					"<td>"+r.rec_title+"</td>" +
					"<td>"+r.rec_phone+"</td>" +
					"<td>"+r.rec_pay_min+"--"+r.rec_pay_max+"</td>" +
					"<td>"+r.rec_duty+"</td>" +
					"<td>"+r.rec_name+"</td>" +
					"<td>"+rec_workYear+"</td>" +
					"<td>"+rec_qualification+"</td>" +
					"<td><textarea class='form-control' rows='2' readonly='readonly'>"+r.rec_welfare+"</textarea></td>" +
					"<td><textarea class='form-control' rows='2' readonly='readonly'>"+(r.rec_company_detail==null?'暂无说明':r.rec_company_detail)+"</textarea></td>" +
					"<td>"+(r.rec_msg_state==1?"上架":"下架")+"</td>" +
					"<td>"+rec_msg_check_state+"</td>" +
					"<td>"+r.rec_company_name+"</td>" +
					"<td>"+r.rec_company_address+"</td>" +
					"<td>"+dateUtil(r.create_time)+"</td>" +
					"<td>" +
						"<input type='button' value='删除' class='btn btn-primary' onclick=removeMessage("+r.rec_id+")>" +
					"</td>" +
				"</tr>";
		});
		zpCon+= "<tr><td colspan='16' align='center'>当前页:"+pageNum+"/总页数:"+pageTotal+"</td></tr>";
		zpCon+= "<tr>" +
				"<td colspan='4'><input type='button' class='btn btn-primary' value='发布招聘信息' onclick='showJobMessage()'></td>" +
				"<td colspan='12' style='text-align:left;padding-left:21%;'>" +
				"<input type='button' value='首页' class='btn btn-primary' onclick=changeZhaoPingMessagePage(this)> " +
				"<input type='button' value='上一页' class='btn btn-primary' onclick=changeZhaoPingMessagePage(this)> " +
				"<input type='button' value='下一页' class='btn btn-primary' onclick=changeZhaoPingMessagePage(this)> " +
				"<input type='button' value='末页' class='btn btn-primary' onclick=changeZhaoPingMessagePage(this)> " +
			"</td></tr>";
	}else{
		zpCon+= "<tr><td align='center' width='1470'><font size='6' color='red'>暂无招聘信息</font></td></tr>"+
				"<tr><td align='center'><input type='button' value='发布招聘信息' onclick='showJobMessage()' class='btn btn-primary'></td></tr>";
	}
	$("#recTable").html(zpCon);
}

function showJobMessage(){
	$("#insertRecTable").show();
	$("#recTable").hide();
}


function changeZhaoPingMessagePage(op){
	var butCon = $(op).val();
	switch(butCon){
	case "首页":
		pageNum=1;
		loadAllRecMsg();
		break;
	case "上一页":
		if(pageNum>1){
			pageNum--;
			loadAllRecMsg();
		}	
		break;
	case "下一页":
		if(pageNum<pageTotal){
			pageNum++;
			loadAllRecMsg();
		}
		break;
	case "末页":
		pageNum=pageTotal;
		loadAllRecMsg();
		break;
	}
}

function removeMessage(rec_id){
	utilAjax("/recruitment_msg/removeRecMsgByRecruiter_id","rec_id="+rec_id,"post",
			function(flag){
		if(flag){
			alert("删除成功");
			loadAllRecMsg();
		}else{
			alert("删除失败");
		}
	});
}

function loadAlter(){
	var alterCon = $("#alterContent").text();
	if(alterCon!=""){
		$(".alterDiv").show();
		var interval = setInterval(function(){
			$(".alterDiv").hide();
			$("#alterContent").text("");
			clearInterval(interval);
		},3000);
	}
	$("#alterBut").click(function(){
		$(".alterDiv").hide();
	});
}

function utilAjax(url,data,type,success){
	$.ajax({
		url:url,
		data:data,
		dataType:'json',
		type:type,
		success:success,
		error:function(){
		}
	});
}
function dateUtil(times){
	var date = new Date(times);//时间戳为10位需*1000，时间戳为13位的话不需乘1000
	Y = date.getFullYear() + '-';
	M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date
			.getMonth() + 1)
			+ '-';
	D = date.getDate() + ' ';
	var dateCon = Y + M + D;
	return dateCon;
}